package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseFinishEntity;
import com.taomu.app.zongchouhuli.entity.OnlyFinishOrder;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.framework.BaseFragment;
import com.taomu.app.zongchouhuli.framework.BasePresenter;
import com.taomu.app.zongchouhuli.moudle.mine.fragment.OrderListFragment;
import com.taomu.app.zongchouhuli.moudle.mine.fragment.WalletListFragment;
import com.taomu.app.zongchouhuli.utils.DpUtils;
import com.taomu.app.zongchouhuli.utils.RxBus2;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UserOrderActivity extends BaseActivity {

    private ImageView mBackIm;

    private MagicIndicator indicator;

    private ViewPager mViewPager;

    private List<String> mTitles = new ArrayList<>();

    private List<BaseFragment> mFragments = new ArrayList<>();

    public static final String CURRENTPAGE = "currentpage";

    public static void start(Context mContext,int page){
        Intent intent = new Intent(mContext,UserOrderActivity.class);
        intent.putExtra(CURRENTPAGE, page);
        mContext.startActivity(intent);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_order;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.order_back_im);
        indicator = findViewById(R.id.order_indicator);
        mViewPager = findViewById(R.id.order_viewpager);
    }

    @Override
    protected void initData() {

        setIndicator();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(getIntent() != null){
            page = getIntent().getIntExtra(CURRENTPAGE, -1);
        }
        if(mViewPager != null){
            if(page != -1){
                mViewPager.setCurrentItem(page);
            }
        }
    }

    private int page = -1;

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        RxBus2.getInstance().toObservable(BaseFinishEntity.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseFinishEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseFinishEntity baseFinishEntity) {
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        RxBus2.getInstance().toObservable(OnlyFinishOrder.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OnlyFinishOrder>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(OnlyFinishOrder baseFinishEntity) {
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private void setIndicator(){
        mTitles.add("全部");
        mTitles.add("待支付");
        mTitles.add("审核中");
        mTitles.add("已取消");
        mTitles.add("已完成");
        mFragments.add(OrderListFragment.getInstence("0"));
        mFragments.add(OrderListFragment.getInstence("order_wait"));
        mFragments.add(OrderListFragment.getInstence("order_wait_confirm"));
        mFragments.add(OrderListFragment.getInstence("order_cancel"));
        mFragments.add(OrderListFragment.getInstence("order_complete"));
        if (mViewPager  == null) {
            return;
        }
        FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        initPagerIndicator(indicator,mViewPager);
        bind(indicator,mViewPager);
        if(page != -1){
            mViewPager.setCurrentItem(page);
        }
    }

    public void bind(final MagicIndicator magicIndicator, ViewPager viewPager) {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });
    }


    /**
     * 初始话导航栏
     * @param indicator
     * @param viewPager
     */
    private void initPagerIndicator(MagicIndicator indicator, final ViewPager viewPager){
        if (indicator == null) {
            return;
        }
        CommonNavigator navigator = new CommonNavigator(this);
        navigator.setBackgroundColor(getResources().getColor(R.color.base_white));
        navigator.setAdjustMode(true);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int i) {
                ColorTransitionPagerTitleView titleView = new ColorTransitionPagerTitleView(context);
                titleView.setText(mTitles.get(i));
                titleView.setNormalColor(getResources().getColor(R.color.text_normal_black));
                titleView.setSelectedColor(getResources().getColor(R.color.base_blue));
                titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.text_28));
                titleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        resetState();
//                        bottomMenu.setVisibility(View.GONE);
//                        if(i == 1){
//                            clearTv.setVisibility(View.GONE);
//                        }else{
//                            clearTv.setVisibility(View.VISIBLE);
//                        }
                        viewPager.setCurrentItem(i);
                    }
                });
                return titleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.base_blue));
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineWidth(DpUtils.dp2px(20));
                return indicator;
            }
        });
        indicator.setNavigator(navigator);
    }
}
